package com.xiaogao.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaogao.statistics.client.UcenterClient;
import com.xiaogao.statistics.entity.StatisticsDaily;
import com.xiaogao.statistics.mapper.StatisticsDailyMapper;
import com.xiaogao.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-07-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    // 远程调用，统计某一天注册人数
    @Override
    public void createStatisticsByDay(String day) {
        // 1、删除已存在的统计对象
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        // 2、获取统计信息
        Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get("countRegister"); // 每日注册人数
        // RandomUtils.nextInt()：返回两个整数之间的随机值
        Integer loginNum = RandomUtils.nextInt(100, 200);// 每日登录数 TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);// 每日播放视频数 TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);// 每日新增课程数 TODO

        // 3、创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        // 最后添加到数据库中
        baseMapper.insert(daily);

    }

    // 图标显示,返回日期json数组,数量json数组
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();

        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            // 封装日期集合
            dateList.add(daily.getDateCalculated());
            // 封装对应数量
            switch (type){
                case "login_num":
                    numList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);

        return map;
    }
}
